package com.voice.call.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.voice.call.common.Const;
import com.voice.call.common.ConstLang;
import com.voice.call.common.ConstUtil;
import com.voice.call.common.Result;
import com.voice.call.common.ResultEnum;
import com.voice.call.entity.VersionUpdate;
import com.voice.call.mapper.VersionUpdateMapper;
import com.voice.call.service.OpLogService;
import com.voice.call.service.VersionUpdateService;



@Service
public class VersionUpdateServiceImpl implements VersionUpdateService {
	public Logger log = LoggerFactory.getLogger(VersionUpdateServiceImpl.class);
	@Resource
	private VersionUpdateMapper updateMapper;
	
	@Resource
	private OpLogService opLogService;
	
	@Override
	public Result checkUpdate(Map<String, Object> paramsMap,String ip) {
		
		String pv = (String) paramsMap.get("pv");
		//String versionName = (String) paramsMap.get("versionName");
		String versionCode = (String) paramsMap.get("versionCode");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pv", pv);
		params.put("status", Const.Y);
		params.put("lang", ConstLang.getLang(paramsMap));
		params.put("versionCode", versionCode);
		
		
		List<VersionUpdate> updateInfoList = updateMapper.list(params);
		if(updateInfoList!=null && !updateInfoList.isEmpty()){
			
			//写日志
			opLogService.updateWriteLog(ConstUtil.getVersionName(paramsMap), ip);
			
			VersionUpdate updateInfo = updateInfoList.get(0);
			
			Map<String, Object> resultParams = new HashMap<String, Object>();
			resultParams.put("pv", updateInfo.getPv());
			resultParams.put("versionName", updateInfo.getVersionName());
			resultParams.put("versionCode", updateInfo.getVersionCode());
			resultParams.put("downUrl", updateInfo.getDownUrl());
			resultParams.put("versionDesc", updateInfo.getVersionDesc());
			resultParams.put("type", updateInfo.getType());
			resultParams.put("gap", updateInfo.getGap());
			return new Result(resultParams);
			
		}else{
			return new Result(ResultEnum.LATEST_VERSION);
		}
		
	}
	
	
	

}
